package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.CzCategoryMapper;
import com.zhentao.pojo.CzCategory;
import com.zhentao.service.CzCategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【cz_category】的数据库操作Service实现
* @createDate 2025-08-13 15:15:07
*/
@Service
public class CzCategoryServiceImpl extends ServiceImpl<CzCategoryMapper, CzCategory>
    implements CzCategoryService{
// 获取一级分类
    @Override
    public List<CzCategory> getLevelOneCategories() {
        LambdaQueryWrapper<CzCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CzCategory::getParentId, 0);
        return list(queryWrapper);
    }
// 获取二级分类

    @Override
    public List<CzCategory> getChildCategoriesByParentId(Integer parentId) {
        LambdaQueryWrapper<CzCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CzCategory::getParentId, parentId);
        return list(queryWrapper);
    }





// 获取分类树
    @Override
    public List<CzCategory> getCategoryTree() {
        // 获取一级分类
        List<CzCategory> levelOneList = getLevelOneCategories();
        // 获取二级分类
        for (CzCategory levelOne : levelOneList) {
            List<CzCategory> levelTwoList = getChildCategoriesByParentId(levelOne.getCatId());
            levelOne.setChildren(levelTwoList);
            // 获取三级分类
            for (CzCategory levelTwo : levelTwoList) {

                List<CzCategory> levelThreeList = getChildCategoriesByParentId(levelTwo.getCatId());
                levelTwo.setChildren(levelThreeList);
            }
        }
        return levelOneList;
    }



}




